﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Dao;

namespace Web.Page
{
    public partial class WLTree : System.Web.UI.Page
    {
        public const int ROOT = 0;
        public const int WL = 1;
        public const int WLSX = 2;
        public const int WLSXZ = 3;

        public const string LEVEL = "level";
        public const string VPATH = "vpath";

        protected int level = -1;
        protected string vpath = null;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                object objlevel = Cache.Get(LEVEL);
                if (objlevel == null)
                {
                    TreeNode root = initRoot();
                }
                else
                {
                    level = (int)objlevel;
                    vpath = (string)Cache.Get(VPATH);
                    rePopulateNode(level, vpath);
                    level = -1;
                    vpath = null;
                    Cache.Remove(LEVEL);
                }
            }
        }

        private TreeNode initRoot()
        {
            tvWL.Nodes.Clear();

            TreeNode root = new TreeNode();
            root.Text = "所有物料类型";
            root.Value = "root";
            root.PopulateOnDemand = false;
            root.NavigateUrl = string.Format("CS_WLLX_Query.aspx?vpath=root");
            root.ImageUrl = "../images/wl/home.png";
            root.ExpandAll();

            tvWL.Nodes.Add(root);
            populateNode(root);

            return root;
        }

        private void loadWLLX(TreeNode node)
        {
            CS_WLLX_Dao dao = new CS_WLLX_Dao();
            List<CS_WLLX> wls = dao.QueryAll();

            node.ChildNodes.Clear();

            foreach (CS_WLLX wl in wls)
            {
                TreeNode child = new TreeNode();
                child.Text = string.Format("{0} {1}", wl.BH.Trim(), wl.MC);
                child.Value = wl.BH;
                child.PopulateOnDemand = true;
                child.NavigateUrl = string.Format("CS_WLLXSX_Query.aspx?wllxbh={0}&vpath={1}", child.Value, node.ValuePath + "/" + child.Value);
                child.ImageUrl = "../images/wl/tools.png";
                
                node.ChildNodes.Add(child);

                if (vpath != null && vpath.StartsWith(child.ValuePath))
                {
                    child.ExpandAll();
                    populateNode(child);
                }
            }
        }


        private void loadWLSX(string wllxbh, TreeNode node)
        {
            CS_WLLXSX_Dao dao = new CS_WLLXSX_Dao();
            List<CS_WLLXSX> wlsxs = dao.Query(wllxbh);

            node.ChildNodes.Clear();

            foreach (CS_WLLXSX wlsx in wlsxs)
            {
                TreeNode child = new TreeNode();
                child.Text = string.Format("{0} {1}", wlsx.BH.Trim(), wlsx.MC);
                child.Value = wlsx.BH;
                child.PopulateOnDemand = true;
                child.NavigateUrl = string.Format("CS_WLSXZ_Query.aspx?wllxbh={0}&wlsxbh={1}&vpath={2}", node.Value, child.Value, node.ValuePath + "/" + child.Value);
                child.ImageUrl = "../images/wl/bullets.png";

                node.ChildNodes.Add(child);

                if (vpath != null && vpath.StartsWith(child.ValuePath))
                {
                    child.Expanded = true;
                    populateNode(child);
                }
            }
        }


        private void loadWLSXZ(string wllxbh, string wlsxbh, TreeNode node)
        {
            CS_WLSXZ_Dao dao = new CS_WLSXZ_Dao();
            List<CS_WLSXZ> sxzs = dao.Query(wllxbh, wlsxbh);

            node.ChildNodes.Clear();

            foreach (CS_WLSXZ sxz in sxzs)
            {
                string[] values = node.ValuePath.Split('/');

                TreeNode child = new TreeNode();
                child.Text = string.Format("{0} {1}", sxz.BH.Trim(), sxz.MC); ;
                child.Value = sxz.BH;
                child.PopulateOnDemand = false;
                child.NavigateUrl = string.Format("CS_WLSXZ_Edit.aspx?bh={0}&mc={1}&wllxbh={2}&wlsxbh={3}&vpath={4}", sxz.BH, sxz.MC, values[WL], values[WLSX], node.ValuePath);
                child.ImageUrl = "../images/wl/bullet_black.png";

                node.ChildNodes.Add(child);
            }
        }

        protected void tvWL_TreeNodePopulate(object sender, TreeNodeEventArgs e)
        {
            if (IsCallback)
            {
                populateNode(e.Node);
            }
        }

        protected void tvWL_TreeNodeExpanded(object sender, TreeNodeEventArgs e)
        {
            populateNode(e.Node);
        }

        private void populateNode(TreeNode node)
        {
            if (node == null)
            {
                initRoot();
                return;
            }

            switch (node.Depth)
            {
                case ROOT:
                    loadWLLX(node);
                    break;

                case WL:
                    loadWLSX(node.Value, node);
                    break;

                case WLSX:
                    string[] values = node.ValuePath.Split('/');
                    loadWLSXZ(values[WL], node.Value, node);
                    break;
            }
        }

        private void rePopulateNode(int level, string vPath)
        {
            populateNode(null);

            //foreach (TreeNode node in tvWL.Nodes)
            //{
            //    if (vPath.Equals(node.ValuePath))
            //    {
            //        node.ChildNodes.Clear();
            //        populateNode(node);
            //    }
            //}
        }

        protected void btnRefresh_Click(object sender, ImageClickEventArgs e)
        {
            //string args = Request.Form["__EVENTTARGET"];
            //lbResult.Text = "args: " + args;
        }

        protected void UpdatePanel1_Load(object sender, EventArgs e)
        {
            string target = Request.Form["__EVENTTARGET"];
            if (("btnRefresh".Equals(target)))
            {
                string args = Request.Form["__EVENTARGUMENT"];
                string a = "args: " + args;
                TreeNode root = initRoot();
                loadWLLX(root);
            }
        }
    }
}